#include <iostream>
#include <string>
using namespace std;
#include <vector>

//vector容器中存放自定义的数据类型
class Person
{
public:
    string name;
    int age;

    Person(string name, int age)
    {
        this->name = name;
        this->age = age;
    }
};

void test1()
{
    vector<Person> v;

    Person p1("孙悟空", 9999);
    Person p2("猪八戒", 999);
    Person p3("唐僧", 30);
    Person p4("白龙马", 1000);
    Person p5("如来佛祖", 999999);

    //向容器中添加数据
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);

    //遍历容器中的数据
    for (vector<Person>::iterator it = v.begin();it != v.end();it++)
    {
        cout << "姓名：" << (*it).name << endl;
        cout << "年龄：" << it->age << endl;
    }
}

//存放自定义数据类型 指针
void test2()
{
    vector<Person*> v;

    Person p1("孙悟空", 9999);
    Person p2("猪八戒", 999);
    Person p3("唐僧", 30);
    Person p4("白龙马", 1000);
    Person p5("如来佛祖", 999999);

    //向容器中添加数据
    v.push_back(&p1);
    v.push_back(&p2);
    v.push_back(&p3);
    v.push_back(&p4);
    v.push_back(&p5);

    //遍历容器
    for (vector<Person*>::iterator it = v.begin();it != v.end();it++)
    {
        cout << "姓名：" << (*it)->name << endl;
        cout << "年龄：" << (*it)->age << endl;
    }
}

int main()
{
    //test1();
    test2();
    return 0;
}